package com.lm.mapstruct.mapper;

import com.lm.mapstruct.dto.UserDTO;
import com.lm.mapstruct.entry.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Mapper(componentModel = "spring",imports = {LocalDateTime .class, Date .class, ZoneId .class})
public interface UserMapper {

    // 将 User 转换为 UserDTO
    @Mapping(source = "id",target = "userId")
    @Mapping(source = "name", target = "userName")
    @Mapping(source = "email", target = "userEmail")
    @Mapping(source = "address.city", target = "city")
    UserDTO userToUserDTO(User user);

    // 将 UserDTO 转换为 User
    @Mapping(source = "userId", target = "id")
    @Mapping(source = "userName", target = "name")
    @Mapping(source = "userEmail", target = "email")
    @Mapping(target = "address.city", source = "city")
    User userDTOToUser(UserDTO userDTO);

    @Mapping(source = "email", target = "userEmail", qualifiedByName = "emailToUppercase")
    UserDTO userToUserDTOByEmail(User user);

    @Named("emailToUppercase")
    static String emailToUppercase(String email) {
        return email.toUpperCase();
    }
}
